#!/bin/bash
# Building WHERE_LIBRARY-download
# ======================================================================
# Laboratoire Jacques-Louis Lions
# Université Pierre et Marie Curie-Paris6, UMR 7598, Paris, F-75005 France
# ======================================================================
# This file is part of Freefem++
#
# Freefem++ is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# Freefem++ is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Freefem++; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# ======================================================================
# headeralh brief="Building WHERE_LIBRARY-download" default=0 freefem multipleauthors shell start=21/09/09 upmc

ff=$0;
bb=`basename $0`
DIR=`dirname $ff`
pp=`pwd`
DESTDIR=""
if [ -n "$1" ] ; then
DESTDIR="$1/"
elif [ $bb = $ff -a  \( '.' = "$DIR" -o -z "$DIR" \) ] ;  then
  ff=`which $0`
  DIR=`dirname $ff`
  if [ '.' = "$DIR" -o -z "$DIR" ] ;  then
     echo Sorry cant find the Directory  place of $0
  fi
fi
case $DIR in
/*) ;;
[.])  DIR=$pp;;
*)  DIR="$pp/$DIR"
esac
DIRP=`dirname $DIR`
DIRP=`dirname $DIRP`
#echo "WWWWW"  $DIRP
if [ -d "$DESTDIR$DIRP/3rdparty/lib" -a -d "$DESTDIR$DIRP/3rdparty/include"  ] ; then
    DIR="$DIRP/3rdparty"
    DIRW="$DIRP/plugin/seq"
elif [ -d "$DESTDIR$DIRP/lib" -a -d "$DESTDIR$DIRP/include"  ] ; then
    DIR="$DIRP"
    DIRW="$DIR/lib"
else
  DIR=@ff_prefix_dir@
  DIRW="$DIRW/lib"
fi

if [   -d $DESTDIR$DIR/lib -a -d $DESTDIR$DIR/include  ] ; then
    LIB="$DIR/lib"
    INC="$DIR/include"
    cd "$DESTDIR$LIB"
#  set -x
    for i in $LIB/WHERE.* ; do
	test -f $i && ( sed "s;@DIR@;$DIR;g" <$i  )
    done
    # ALH - 6/1/14 - remove umfpack and amd because some versions of umfpack needs more than just adding -L (they need
    # the colamd and cholmod libraries, which are setup in [[file:configure.ac]]).
    cd "$DIRW"
    for la in arpack fftw3 mmg3d; do
        l=`echo $la |sed -e s/^tet$/tetgen/`
	nbw=`awk "/^$la /" WHERE_LIBRARY WHERE_LIBRARY-config|wc -l`
       if [ "$nbw" -ne 0 -a ! -f $LIB/WHERE.$l  ]; then
	   if [ -z "$l" ];then l="$la";fi
	   if  ( ls  "lib$la"[-._0-9]*a 1>/dev/null 2>/dev/null ) ; then
	       ll=`ls  "lib$la"[-._0-9]*a|grep [.]a`
	       if [ -n "$ll" ]; then
		   lib=`expr "$ll" : lib'\(.*\).a'`
		   echo $l LD "'"-L$LIB"'" "'"-l$lib"'"
		   if [ -d $INC/$la ] ; then
		       echo $l INCLUDE "'"-I$INC/$la"'"
		   else
		       echo $l INCLUDE "'"-I$INC"'"
		   fi
	       fi
	   fi
       fi
    done
else
    echo error not directory $DIR/lib and $DIR/include  $DIRP
fi

# Local Variables:
# mode:shell-script
# ispell-local-dictionary:"british"
# coding:utf-8
# End:
